package com.eastjet.equipment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastjet.equipment.service.DeviceInspectiontaskService;
import com.eastjet.equipment.service.DevicePmcsService;
import com.eastjet.equipment.service.InspectionDetailsService;
import com.eastjet.maintenance.domain.DeviceInspectiontask;
import com.eastjet.maintenance.domain.DevicePmcs;
import com.eastjet.maintenance.domain.DeviceSpotcheck;
import com.eastjet.maintenance.domain.InspectionDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/spotTask")
public class DeviceSpotchecktaskController {

    @Autowired
    private DevicePmcsService devicePmcsService;
    @Autowired
    private InspectionDetailsService inspectionDetailsService;

    @GetMapping("/taskList")
    public Page<DevicePmcs> getSupplierList(@RequestParam(defaultValue = "1") int page,
                                            @RequestParam(defaultValue = "2") int size,
                                            @RequestParam(defaultValue = "") String condition,
                                            @RequestParam(defaultValue = "")String name
    ) {
        System.out.println("condition:" + condition);
        System.out.println("page:" + page);
        System.out.println("name:"+name);
        return devicePmcsService.getPmcsList(page, size, condition,name);
    }


    @GetMapping("/detail")
    public List<InspectionDetails> selectDetail(@RequestParam(defaultValue = "") String code) {
        return inspectionDetailsService.selectDetail(code);
    }


    @RequestMapping("/updateTask")
    public String updateTask(@RequestBody Map<String, Object> map) throws ParseException {
        System.out.println("taskmap:" + map);
        DevicePmcs devicePmcs = new DevicePmcs();
        devicePmcs.setTaskCoding((String) map.get("taskCoding"));
        devicePmcs.setPmcsTime((String) map.get("pmcsTime"));
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date inspectiontaskOkontime = sdf.parse((String) map.get("pmcsOkontime"));
        Date inspectiontaskOkuntime = sdf.parse((String) map.get("pmcsOkuntime"));
        devicePmcs.setPmcsOkontime(inspectiontaskOkontime);
        devicePmcs.setPmcsOkuntime(inspectiontaskOkuntime);
        System.out.println("devicePmcs:" + devicePmcs);
        return devicePmcsService.updatePmcs(devicePmcs);
    }

    @RequestMapping("/updateSkip")
    public String updateSkip(@RequestBody Map<String, Object> map) throws ParseException {
        System.out.println("skipmap:" + map);
        if (map.isEmpty()) {
            return "参数不能为空";
        } else {
            DeviceInspectiontask task = new DeviceInspectiontask();
            task.setCode((String) map.get("code"));
            task.setCause((String) map.get("cause"));
            task.setInspectiontaskSkip((Integer) map.get("skipNumber"));
            System.out.println("task:" + task);
            return "";
        }

    }

    @RequestMapping("/adjustTask")
    public String adjustTask(@RequestBody Map<String, Object> map) {
        System.out.println("adjustmap:" + map);
        if (map.isEmpty()) {
            return "参数不能为空";
        } else {
            DevicePmcs task = new DevicePmcs();
            task.setTaskCoding((String) map.get("code"));
            task.setCause((String) map.get("cause"));
            DeviceSpotcheck spotcheck = new DeviceSpotcheck();

            spotcheck.setSpotcheckPerson((String) map.get("person"));
            spotcheck.setSpotcheckClassId((String) map.get("inspectiontaskClass"));
            spotcheck.setCode((String) map.get("spotcheckCode"));
            task.setDeviceSpotcheck(spotcheck);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date inspectiontaskOkontime = sdf.parse((String) map.get("inspectiontaskOkontime"));
                task.setPmcsOkontime(inspectiontaskOkontime);
                Date inspectiontaskOkuntime = sdf.parse((String) map.get("inspectiontaskOkuntime"));
                task.setPmcsOkuntime(inspectiontaskOkuntime);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }

            System.out.println("task:" + task);
            return devicePmcsService.adjustPmcs(task);
        }
    }

}
